package com.imddysc.dwz.controller;

import com.imddysc.dwz.entity.Links;
import com.imddysc.dwz.service.LinksService;
import com.imddysc.dwz.utils.DecimalUtils;
import com.imddysc.dwz.utils.Result;
import com.imddysc.dwz.utils.ResultUtils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.view.RedirectView;

import java.time.LocalDateTime;

@Controller
public class TController {
	
	private static final Logger logger = LoggerFactory.getLogger(TController.class);

	@Autowired
	private LinksService linksService;

	@RequestMapping("/test/{keyword}")
	public RedirectView testDwz(@PathVariable("keyword") String keyword) {
		Links links = new Links();
		links.setId(916132832L);
		//links.setUrl("http://wiki.msxf.com/pages/viewpage.action?pageId=7683488");
		links.setUrl("https://www.baidu.com");
		links.setKeyword("1000");
		links.setType("custom");
		links.setInsertAt(LocalDateTime.now());
		links.setUpdatedAt(LocalDateTime.now());
		
		RedirectView redirectView = new RedirectView();
		
		if("1000".equals(keyword)) {
			redirectView.setUrl(links.getUrl());
			//redirectView.setStatusCode(HttpStatus.MOVED_PERMANENTLY);
		} else {
			redirectView.setUrl("/error");
		}
		
		return redirectView;
	}


	@RequestMapping("/{keyword}")
	public RedirectView dwz(@PathVariable("keyword") String keyword) {

		Long urlId = DecimalUtils._62_to_10(keyword);
		Links links = linksService.getById(urlId);

		RedirectView redirectView = new RedirectView();
		if(null != links) {
			redirectView.setUrl(links.getUrl());
			//redirectView.setStatusCode(HttpStatus.MOVED_PERMANENTLY);
		} else {
			redirectView.setUrl("/error");
		}

		return redirectView;
	}

	@ResponseBody
	@RequestMapping("/addurl.do")
	public Result addUrl(String url) {
		try {
			Links newLinks = linksService.addLinks(url);
			return ResultUtils.success(newLinks);
		} catch (Exception exception) {
			return ResultUtils.failure("添加失败");
		}
	}
	
	@ResponseBody
	@RequestMapping("/geturl.do")
	public Result getUrl(Long id) {
		try {
			Links newLinks = linksService.getLinks(id);
			return ResultUtils.success(newLinks);
		} catch (Exception exception) {
			return ResultUtils.failure("获取失败");
		}
	}

	@ResponseBody
	@RequestMapping("/updateurl.do")
	public Result updateUrl(String oldurl, String newurl) {
		try {
			Links newLinks = linksService.updateLinks(oldurl, newurl);
			return ResultUtils.success(newLinks);
		} catch (Exception exception) {
			return ResultUtils.failure("修改失败");
		}
	}
	
	@ResponseBody
	@RequestMapping("/update0url.do")
	public Result update0Url(Long id, String newurl) {
		try {
			Links links = linksService.getById(id);
			links.setUrl(newurl);
			Links newLinks = linksService.saveAndFlushLinks(links);
			return ResultUtils.success(newLinks);
		} catch (Exception exception) {
			return ResultUtils.failure("修改失败");
		}
	}

	@ResponseBody
	@RequestMapping("/deleteurl.do")
	public Result deleteUrl(String url) {
		try {
			linksService.deleteLinks(url);
			return ResultUtils.success("删除成功");
		} catch (Exception exception) {
			return  ResultUtils.failure("删除失败");
		}
	}
	
	@ResponseBody
	@RequestMapping("/delete0url.do")
	public Result delete0Url(Long id) {
		try {
			linksService.deleteLinksById(id);
			return ResultUtils.success("删除成功");
		} catch (Exception exception) {
			return  ResultUtils.failure("删除失败");
		}
	}

	@ResponseBody
	@RequestMapping("/batchadd.do")
	public Result batchAdd(String[] urls) {
		try {
			for (String url: urls) {
				linksService.addLinks(url);
			}
			return ResultUtils.success("批量添加成功");
		} catch (Exception exception) {
			return ResultUtils.failure("批量添加失败");
		}
	}

	@ResponseBody
	@RequestMapping("/batchdel.do")
	public Result batchDel(Long[] ids) {
		try {
			for (Long id: ids) {
				linksService.deleteLinksById(id);
			}
			return ResultUtils.success("批量删除成功");
		} catch (Exception exception) {
			return  ResultUtils.failure("批量删除失败");
		}
	}
	
	@ResponseBody
	@RequestMapping("/findAllPageabe.do")
	public Page<Links> findAllByPageable(Integer start, Integer length) {
		Integer currentPage = (int)Math.floor(start/length);
		Integer pageSize = length;
		Page<Links> pageLinks = linksService.findAllByPageable(currentPage, pageSize);
		logger.info(pageLinks.toString());
		return pageLinks;
	}
	
	@ResponseBody
	@RequestMapping("/findAllPageabe1.do")
	public Page<Links> findAllByPageable1(Integer pageNumber, Integer pageSize) {
		Page<Links> pageLinks = linksService.findAllByPageable(pageNumber, pageSize);
		logger.info(pageLinks.toString());
		return pageLinks;
	}
	
	@ResponseBody
	@RequestMapping("/findAllPageabe2.do")
	public Page<Links> findAllByPageable2(Integer pageNumber, Integer pageSize) {
		Page<Links> pageLinks = linksService.findAllByPageable(pageNumber, pageSize);
		logger.info(pageLinks.toString());
		return pageLinks;
	}
}
